package fu.abc.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import fu.abc.DAO.LocationDAO;
import fu.abc.model.Location;

/**
 * Servlet implementation class CreateLocationUser
 */
@WebServlet("/CreateLocationUser")
public class CreateLocationUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateLocationUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		Location location = new Location();
		LocationDAO localDAO = new LocationDAO();
		Random rand = new Random(); 
		int value = rand.nextInt(10000); 
		int status = 1;
		boolean mode = true;
		
//		String remoteAddr = request.getRemoteAddr();
//		ReCaptchaImpl recap = new ReCaptchaImpl();
//		recap.setPrivateKey("6LcdsfgSAAAAAC3-Hd6mN61BZ5qtSAQsi4UgZgpv");
//		int count =1;
//		for(int i=0;i<10000;i++){
//			count = count+i;
//		}
		
		String localName = request.getParameter("localname").toString().trim();
		String category = request.getParameter("category").toString().trim();
		String province = request.getParameter("province").toString().trim();
		String addressDetail = request.getParameter("addresslocal").toString().trim();
		String phone = request.getParameter("phone").trim();
		String website = request.getParameter("website").trim();
		String descript = request.getParameter("descript").toString().trim();
		String email = request.getParameter("email").trim();
		String image = request.getParameter("image").trim();
//		String requestcapt = request.getParameter("recaptcha_challenge_field");
//		String responsecapt = request.getParameter("recaptcha_response_field");
//		ReCaptchaResponse recaptcha = recap.checkAnswer(remoteAddr, requestcapt, responsecapt);
		
		if("".equals(phone) || phone==null){
			phone = "N/A";
		}else if("".equals(email) || email==null){
			email = "N/A";
		}else if("".equals(website) || website==null){
			website = "N/A";
		}
		
			location.setLocationName(localName);
			location.setCategory(category);
			location.setProvince(province);
			location.setAddressDetail(addressDetail);
			location.setPhone(phone);
			location.setWebsite(website);
			location.setDescript(descript);
			location.setStatus(status);
			location.setLinkimage(image);
			location.setEmail(email);
			location.setEncode(value);
			location.setMode(mode);
			
			try {
				localDAO.createLocationUser(location);
				response.sendRedirect("/Vnplace/end-user/acceptlocal.jsp?name='"+localName+"'");
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}

}
